/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswing;

import javax.swing.JSlider;

import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreSlider;

/**
 * A JSlider (javax.swing.JSlider) that conforms to the SpaglCoreSlider interface
 * @author Sam Pottinger
 */
public class SpaglSlider extends JSlider implements SpaglCoreSlider {

	private static final long serialVersionUID = 1L;
	private SpaglCoreContainer parentWidget;
	public static final int VERTICAL = JSlider.VERTICAL;
	public static final int HORIZONTAL = JSlider.HORIZONTAL;

	/**
	 * Creates a slider using the default layout behavior
	 * @param parent The parent widget that will hold this object
	 * @param min The minimum integer value that can be selected in this widget
	 * @param max The maximum integer value that can be selected in this widget
	 * @param orientation The integer representation of the orientation of this widget. Either the class constant VERTICAL or HORIZONTAL
	 */
	public SpaglSlider(SpaglCoreContainer parent, int min, int max, int orientation)
	{
		super(min, max);
		parentWidget = parent;
		super.setOrientation(orientation);
	}
	
	/**
	 * Creates a slider using the specified layout parameters
	 * @param parent The parent widget that will hold this object
	 * @param min The minimum integer value that can be selected in this widget
	 * @param max The maximum integer value that can be selected in this widget
	 * @param orientation The integer representation of the orientation of this widget. Either the class constant VERTICAL or HORIZONTAL
	 * @param parameters The string parameters to be used in placing this widget in the layout manager
	 */
	public SpaglSlider(SpaglCoreContainer parent, int min, int max, int orientation, String parameters)
	{
		super(min, max);
		parentWidget = parent;
		super.setOrientation(orientation);
		parentWidget.placeWidget(this, parameters);
	}
	
	/**
	 * Gets the minimum value that this widget can be set to in the current settings
	 * @return The smallest possible integer that can be selected in this widget
	 */
	public int getMin()
	{
		return getMinimum();
	}
	/**
	 * Sets the minimum value that can be selected in this widget
	 * @param newMin the lowest integer value that can be selected in this widget
	 */
	public void setMin(int newMin)
	{
		super.setMinimum(newMin);
	}
	
	/**
	 * Gets the maximum selectable value in this widget
	 * @return An integer representation of the maximum value selectable in this object
	 */
	public int getMax() {
		return getMaximum();
	}

	/**
	 * Sets the maximum value that can be selected in this widget
	 * @param newMax the highest integer value that can be selected in this widget
	 */
	public void setMax(int newMax) {
		super.setMaximum(newMax);
	}

	/**
	 * Simulates disposal of this widget (does not do anything in Swing)
	 */
	public void dispose() {
		// Does not do anything for swing.
	}

	/**
	 * Gets the parent widget that is holding this object
	 * @return The container holding this widget as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}
	
	/**
	 * Adds an event listener to this slider
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param targetObject The object where the method should be fired 
	 */
	public void addEventListener(int eventID, String method, Object targetObject) {
		try{
			switch(eventID)
			{
				case SpaglEvent.VALUE_CHANGED_EVENT: {this.addChangeListener(new SpaglEventListener(targetObject.getClass().getMethod(method, SpaglCoreEvent.class), targetObject, SpaglEvent.VALUE_CHANGED_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}

}
